package com.jiangjiang.eshop.commodity.controller;

import com.google.common.collect.Lists;
import com.jiangjiang.eshop.commodity.dto.GoodsPropertyValueDTO;
import com.jiangjiang.eshop.commodity.service.GoodsPropertyValueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @ClassName GoodsPropertyValueController
 * @Description 商品属性值管理controller组件
 * @Author jhw
 * @Date 2021/8/2 16:14
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/commodity/goods/property/value")
public class GoodsPropertyValueController {
    @Autowired
    private GoodsPropertyValueService goodsPropertyValueService;

    /**
     * 根据商品id查询属性值
     *
     * @param goodsId 商品id
     * @return 查询结果
     */
    @GetMapping("/queryByGoodsId")
    public List<GoodsPropertyValueDTO> queryByGoodsId(@RequestParam("goodsId") Long goodsId) {
        try {
            return goodsPropertyValueService.queryByGoodsId(goodsId);
        } catch (Exception e) {
            log.warn("error", e);
        }
        return Lists.newArrayList();
    }

    /**
     * 新增商品属性值
     *
     * @param propertyValues 商品属性值
     */
    @PostMapping("/batchSave")
    public Boolean batchSave(@RequestBody List<GoodsPropertyValueDTO> propertyValues) {
        try {
            return goodsPropertyValueService.batchSaveProperty(propertyValues);
        } catch (Exception e) {
            log.error("error", e);
            return false;
        }
    }

    /**
     * 根据商品id删除属性值
     *
     * @param goodsId 商品id
     */
    @GetMapping("/removeByGoodsId")
    public Boolean removeByGoodsId(@RequestParam("goodsId") Long goodsId) {
        try {
            return goodsPropertyValueService.removeByGoodsId(goodsId);
        } catch (Exception e) {
            log.error("error", e);
            return false;
        }
    }

}
